﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YesSql.Indexes;
using Ruitu.Edu.TrainingDemo.Models;
using OrchardCore.ContentManagement;

namespace Ruitu.Edu.TrainingDemo.Indexes
{
    public class PersonPartIndex : MapIndex
    {
        public string ContentItemId { get; set; } = string.Empty;

        public DateTime? BirthDateUtc { get; set; }

        public Handedness Handedness { get; set; }
    }

    public class PersonPartIndexProvider : IndexProvider<ContentItem>
    {
        public override void Describe(DescribeContext<ContentItem> context)
        {
            context.For<PersonPartIndex>()
                .When(contentItem => contentItem.Has<PersonPart>())
                .Map(contentItem =>
                {
                    var personPart = contentItem.As<PersonPart>();
                    if (personPart != null)
                    {
                        return new PersonPartIndex
                        {
                            ContentItemId = contentItem.ContentItemId,
                            BirthDateUtc = personPart.BirthDateUtc,
                            Handedness = personPart.Handedness
                        };
                    }
                    return null;
                });
        }

    }

}
